package com.syj.collect.model;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.previewlibrary.ZoomMediaLoader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.syj.collect.Present.PhotoPresent;
import com.syj.collect.adapter.MyCollectAdapter;
import com.syj.collect.adapter.MyPhotoAdapter;
import com.syj.collect.adapter.MyPicAdapter;
import com.syj.collect.adapter.MyPictureAdapter;
import com.syj.collect.app.R;
import com.syj.collect.base.BaseMvpAvtivity;
import com.syj.collect.base.EmptyRecyclerView;
import com.syj.collect.entry.MyPhone;
import com.syj.collect.entry.MyPhotoBean;
import com.syj.collect.utils.DataUtils;
import com.syj.collect.utils.Glide4Engine;
import com.syj.collect.utils.LoadingDialog;
import com.syj.collect.utils.SPUtil;
import com.syj.collect.utils.TextGLde;
import com.syj.collect.view.PhotoActivityView;

import java.io.UnsupportedEncodingException;


import java.util.List;


public class MyPhontoActivity extends BaseMvpAvtivity<PhotoActivityView, PhotoPresent> implements PhotoActivityView {
    private EmptyRecyclerView Rcl;
    private MyPictureAdapter myPicAdapter;
    private  int page=1;
    private SmartRefreshLayout refresh;
    @Override
    public PhotoPresent initPresenter() {
        return new PhotoPresent();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo);
        ZoomMediaLoader.getInstance().init(new TextGLde());
        initTitle("我的照片");
        String token = SPUtil.getPrefString("token", "");
        System.out.println(token);
        Rcl = (EmptyRecyclerView) findViewById(R.id.com_rerec);
        Rcl.setEmptyView(findViewById(R.id.empty_view_));
        refresh = findViewById(R.id.comm_refresh);
        Rcl.setLayoutManager(new LinearLayoutManager(this));
        myPicAdapter = new MyPictureAdapter(this);
        Rcl.setAdapter(myPicAdapter);
        try {
            loadingDialog.create();
            loadingDialog.show();
            presenter.getUserImgList(page);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        refresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                try {
                    presenter.getUserImgList(++page);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                try {
                    page=1;
                    presenter.getUserImgList(1);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void initsuccess(List<MyPhone.DataDTO.ListDTO> data) {
        loadingDialog.dismiss();
        myPicAdapter.addList(data,page);
        if(refresh!=null)refresh.finishLoadMore();
        if(refresh!=null)refresh.finishRefresh();
    }

    @Override
    public void initfail(String msg) {
        loadingDialog.dismiss();
        if(refresh!=null)refresh.finishLoadMore();
        if(refresh!=null)refresh.finishRefresh();
    }


}
